/* ========================================================================
 * Copyright 2007 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless reqired by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================================
 * Created on 11 oct. 2007
 * @author pollux
 */
package org.poxd.frisar.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import org.poxd.frisar.Frisar;

public class DefaultTailComponent extends JComponent
{
    private static final long serialVersionUID = 42L;
    private final int MAX_WIDTH = 100;
    private final int MIN_WIDTH = 30;
    private final Stroke ROUNDED = new BasicStroke(2, BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_ROUND);
    private Frisar frise;
    private int height;
    private int width = 20;
    private Paint bg;

    public DefaultTailComponent(Frisar f)
    {
        frise = f;
        height = frise.getHeight();
        initComponent();
    }

    protected void initComponent()
    {
        bg = Color.lightGray;
    }

    @Override
    public void paintComponents(Graphics g)
    {
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setStroke(ROUNDED);

        double x = 0.0;
        double y = height / 4;
        RoundRectangle2D.Double tail = new RoundRectangle2D.Double(x+1, y, width + 5, 2 * y, 5.0, 5.0);

        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHints(rh);
        GradientPaint p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0,
                height - 1, new Color(100, 100, 100));
        g2.setClip(0, 0, width, height);
        g2.setPaint(p1);
        g2.draw(tail);
        g2.setPaint(bg);
        g2.fill(tail);
    }

    public void setWidth(int width)
    {
        if (this.width != width)
        {
            if (width > MIN_WIDTH && width < MAX_WIDTH)
            {
                firePropertyChange("width", this.width, width);
                this.width = width;
                repaint();
            }
        }
    }
    
    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }
    
    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(width, height);
    }

    @Override
    public void setMaximumSize(Dimension maximumSize)
    {
    }

    @Override
    public void setMinimumSize(Dimension minimumSize)
    {
    }
    
    @Override
    public void setPreferredSize(Dimension preferredSize)
    {
    }
}